﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;

using ERPStore.Extensions;

namespace ERPStore.Web.Logging
{
	public class WebApiLoggerHandler : DelegatingHandler
	{
		public WebApiLoggerHandler()
		{
			this.Logger = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Logging.ILogger>();
		}

		protected ERPStore.Logging.ILogger Logger { get; private set; }

		protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
		{
			if (request.Content != null)
			{
				var buffer = request.Content.ReadAsByteArrayAsync().Result;
				var content = System.Text.Encoding.UTF8.GetString(buffer);
				Logger.Debug("WebApiContent:" + content);
			}

			return base.SendAsync(request, cancellationToken);
		}
	}
}
